package ar.com.jendrix.common.util;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;

public final class IOUtil {
	/**
	 */
	public static final String getFileContent(String file) throws IOException {
		String line;
		BufferedReader br = new BufferedReader(new FileReader(file));
		StringBuilder sb = new StringBuilder();

		while ((line = br.readLine()) != null) {
			sb.append(line);
		}
		br.close();

		// Returns file's contents.
		return sb.toString();
	}

	/**
	 */
	public static final String getFileContent(InputStream is) throws IOException {
		int b;
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		String fc;

		while ((b = is.read()) != -1) {
			baos.write(b);
		}
		fc = baos.toString();
		baos.close();

		// Returns file's contents.
		return fc;
	}

	public static final void closeFile(FileInputStream fis) {
		if (fis != null) {
			try {
				fis.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	public static final void closeFile(InputStream fis) {
		if (fis != null) {
			try {
				fis.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}